package com.cardbaobao.cardbabyclient.ui.pop;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.model.LoanPopFilterList;
import com.cardbaobao.cardbabyclient.utils.ScreenUtil;
import com.cardbaobao.cardbabyclient.widget.MyGridView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jiangzx on 9:28.
 * 借款类型、还款方式
 */

public class LoanTypePop extends PopupWindow {
    private LoanTypeEnsuredListener loanTypeEnsuredListener;
    public void setLoanTypeEnsuredListener(LoanTypeEnsuredListener listener){
        this.loanTypeEnsuredListener=listener;
    }
    public interface LoanTypeEnsuredListener {
        void onLoanTypeEnsured(List<LoanPopFilterList.LoanPopFilter> authTypeSelects,
                               List<LoanPopFilterList.LoanPopFilter> repaymentSelects);
    }

    private Context context;

    private LoanFilterAdapter authTypeAdapter;

    private LoanFilterAdapter repaymentAdapter;

    private TextView tvReset;//重置
    private TextView tvEnsure;//确认

    public LoanTypePop(Context context,
                       @NonNull final List<LoanPopFilterList.LoanPopFilter> authTypeData,
                       @NonNull final List<LoanPopFilterList.LoanPopFilter> repaymentData){
        this.context=context;
        View rootView= LayoutInflater.from(context).inflate(R.layout.pop_loan_type,null);
        rootView.findViewById(R.id.view_empty).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        MyGridView authTypeGridView= (MyGridView) rootView.findViewById(R.id.auth_type_grid);
        authTypeGridView.setNumColumns(4);
        authTypeAdapter =new LoanFilterAdapter(authTypeData);
        authTypeGridView.setAdapter(authTypeAdapter);
        authTypeGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                authTypeAdapter.setOrRemoveSelected(position);
            }
        });

        MyGridView repaymentGridView= (MyGridView) rootView.findViewById(R.id.repayment_grid);
        repaymentGridView.setNumColumns(4);
        repaymentAdapter=new LoanFilterAdapter(repaymentData);
        repaymentGridView.setAdapter(repaymentAdapter);
        repaymentGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                repaymentAdapter.setOrRemoveSelected(position);
            }
        });

        tvReset= (TextView) rootView.findViewById(R.id.tv_reset);
        tvEnsure= (TextView) rootView.findViewById(R.id.tv_ensure);
        initListener();
        setAnimationStyle(R.style.CustomTopAnimateStyle);
        setBackgroundDrawable(new ColorDrawable());
        setOutsideTouchable(false);
        setWidth(ScreenUtil.getScreenWidth(context));
        //设置内容区域高度为屏幕高度的一半
        rootView.findViewById(R.id.pop_content).getLayoutParams().height=
                (int) (ScreenUtil.getScreenHeight(context)*0.5f);
        setContentView(rootView);
    }

    public void setCurrentSelects(List<LoanPopFilterList.LoanPopFilter> loanTypeFilters,
                                  List<LoanPopFilterList.LoanPopFilter> repaymentFilters){
        if(loanTypeFilters!=null){
            for(LoanPopFilterList.LoanPopFilter filter:loanTypeFilters){
                authTypeAdapter.setSelected(filter);
            }
        }
        if(repaymentFilters!=null){
            for(LoanPopFilterList.LoanPopFilter filter:repaymentFilters){
                repaymentAdapter.setSelected(filter);
            }
        }
    }

    private void initListener(){
        tvReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                authTypeAdapter.getSelects().clear();
                authTypeAdapter.notifyDataSetChanged();
                repaymentAdapter.getSelects().clear();
                repaymentAdapter.notifyDataSetChanged();
            }
        });
        tvEnsure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(loanTypeEnsuredListener!=null){
                    loanTypeEnsuredListener.onLoanTypeEnsured(authTypeAdapter.getSelects(),
                            repaymentAdapter.getSelects());
                }
                dismiss();
            }
        });

    }


    private class LoanFilterAdapter extends BaseAdapter{
        private List<LoanPopFilterList.LoanPopFilter> data;
        private LoanFilterAdapter(List<LoanPopFilterList.LoanPopFilter> data){
            this.data=data;
        }
        private List<LoanPopFilterList.LoanPopFilter> selects=new ArrayList<>();
        @Override
        public int getCount() {
            return data==null?0:data.size();
        }

        @Override
        public Object getItem(int position) {
            return data==null?null:data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder;
            if(convertView==null){
                convertView=LayoutInflater.from(context).
                        inflate(R.layout.item_card_screen_gv,parent,false);
                holder=new Holder();
                holder.rlScreen= (RelativeLayout) convertView.findViewById(R.id.rl_screen);
                holder.tvName= (TextView) convertView.findViewById(R.id.tv_name);
                holder.ivMark= (ImageView) convertView.findViewById(R.id.iv_choose);
                convertView.setTag(R.layout.item_card_screen_gv,holder);
            }else{
                holder= (Holder) convertView.getTag(R.layout.item_card_screen_gv);
            }
            holder.tvName.setText(data.get(position).getName());
            if(selects.contains(data.get(position))){
                holder.rlScreen.setBackgroundResource(R.drawable.tv_bg_line_ff9e27_34px);
                holder.tvName.setTextColor(0xffff9e27);
                holder.ivMark.setVisibility(View.VISIBLE);
            }else{
                holder.rlScreen.setBackgroundResource(R.drawable.tv_bg_line_eee_34px);
                holder.tvName.setTextColor(0xff222222);
                holder.ivMark.setVisibility(View.INVISIBLE);
            }
            return convertView;
        }

        private void setOrRemoveSelected(int position){
            LoanPopFilterList.LoanPopFilter itemData=data.get(position);
            if(selects.contains(itemData)){
                selects.remove(itemData);
            }else{
                selects.add(itemData);
            }
            notifyDataSetChanged();
        }

        private void setSelected(LoanPopFilterList.LoanPopFilter itemData){
            if(itemData!=null&&!selects.contains(itemData)){
                selects.add(itemData);
                notifyDataSetChanged();
            }
        }

        private List<LoanPopFilterList.LoanPopFilter> getSelects(){
            return this.selects;
        }

        private class Holder{
            private RelativeLayout rlScreen;
            private TextView tvName;
            private ImageView ivMark;
        }
    }


}
